package com.lfs.notify.controller;

import com.lfs.base.exception.BusinessException;
import com.lfs.common.constant.CommonConstants;
import com.lfs.common.utils.StringUtils;
import com.lfs.dao.service.SystemService;
import com.lfs.interfaces.log.service.LogFileService;
import com.lfs.interfaces.model.NotifyModel;
import com.lfs.interfaces.model.vo.LogFileVO;
import com.lfs.notify.service.NotifyService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

@RestController
@RequestMapping(value = "/manual")
public class ManualNotifyController {

    private static final Logger logger = LoggerFactory.getLogger(ManualNotifyController.class);

    @Resource
    private NotifyService notifyService;

    @Resource
    private LogFileService logFileService;

    /**
     * @param notifyModel
     * @return
     */
    @PostMapping("/manualNotify")
    @ResponseBody
    public String manualNotify(@RequestBody NotifyModel notifyModel) {
        if(null == notifyModel){
            throw new BusinessException("手工回调请求的参数为空!");
        }
        if(StringUtils.isBlank(notifyModel.getOrderNo())){
            throw new BusinessException("手工回调请求的订单号为空");
        }
        if(StringUtils.isBlank(notifyModel.getStatus())){
            throw new BusinessException("手工回调请求的订单状态为空");
        }
        String status =notifyModel.getStatus();
        String upOrderNo = notifyModel.getUpOrderNo();
        String orderNo = notifyModel.getOrderNo();
        logger.info("银行卡手工回调结果    status:" + status + " orderNo:"+orderNo+" upOrderNo:"+upOrderNo);
        String respString=notifyService.notifyBank(orderNo, status, upOrderNo);
        logger.info("银行卡手工回调下游应答：orderNo=[" + orderNo + "]result=[" + respString + "]");
        try {
            String loginName = StringUtils.isBlank(SystemService.getCurrentUser().getLoginName()) ? "error" :SystemService.getCurrentUser().getLoginName();
            LogFileVO logFile = new LogFileVO();
            logFile.setLogType(CommonConstants.LOG_UPDATE_TYPE);
            logFile.setModifyType(CommonConstants.LOG_CALL_BACK_TYPE);
            logFile.setIpAddr(this.getClass().getName());
            String logRemark = "用户id="+SystemService.getCurrentUser().getId() + "更新订单号="+orderNo+"回调状态="+status;
            logFile.setLogRemark(logRemark);
            logFile.setLoginName(loginName);
            logFileService.insertLogFile(logFile);
        } catch (Exception e) {
            logger.info("新增日志操作数据库异常!",e.getMessage());
            return respString;
        }
        return respString;
    }
}
